/**
 * 项目管理--js
 * 
 * @author cl
 */
var project_manage = {
	MODULE_ID : "",
	Table_Obj : null,// 表格对象
	USER_:null,
	init : function() {
		project_manage.USER_= getSessoin();
		project_manage.loadData();
		project_manage.MODULE_ID = GetRequest().pid;
	},
	/**
	 * 初始化
	 */
	loadData : function() {
		var obj = project_manage.getParam();
		$.ajax({
			url : "/crm/retrieval/comSearch", // 请求的url地址
			dataType : "json", // 返回格式为json
			async : true,// 请求是否异步，默认为异步，这也是ajax重要特性
			data : obj, // 参数值
			type : "post", // 请求方式
			success : function(data) {
				project_manage.initTable(data.rows);
				$("#total_records").text(data.total);
				$("#this_page").text(1);
				$("#total_page").text(data.totalPages);
				var url = "/crm/retrieval/comSearch";
				setCommon(obj, Table_Obj, url);
			},
			error : function() {
			}
		})
	},
	/**
	 * 检索获取条件
	 */
	getParam : function() {
		var obj = {};
		obj.pageIndex = 1;
		pageSize = $("#page_size").val();
		obj.pageSize = pageSize;
		obj.orderBy = "create_time";
		obj.orderDesc = 1;
		obj.table = "data_project";
		var text = $("#searchWords").val().replace(/ /g, '@#@');
		obj.text=text;
		obj.higthText="AND,pro_stats,0,0";
		return obj;
	},
	/**
	 * 初始化表格
	 */
	initTable:function(datas) {
		var esay = $("#TableContainer").easyTable({
			data : datas, // 初始数据，动态添加可以通过setData
			hideCheckbox : false, // 否显示复选框，获取复选框选择的数据用方法getCheckedItem
			clickChangeColor:false,//单击之后切换颜色，
			doubleClickChangeColor:true,//双击之后切换颜色，
			// rowClick : function(data) {// 行点击回调，参数为改行数据
			// alert(JSON.stringify(data))
			// },
			rowDoubleClick : function(data) {// 行双击回调，参数为改行数据
				project_manage.lookDetail(data.uuid);
			},
			columns : [ {
				title : "项目名称",
				field : "pro_name"
			},{
				title : "企业名称",
				field : "ent_name"
			},{
				title : "负责人",
				field : "liable_user_name"
			}, {
				title : "小组成员",
				field : "team_members_names"
			}, {
				title : "开始时间",
				field : "begin_time"
			}, {
				title : "下一节点时间",
				field : "next_step_time"
			}, {
				title : "创建人",
				field : "create_user_name"
			}, {
				title : "创建时间",
				field : "create_time"
			}, {
				title : "项目描述",
				display : "none",
				field : "pro_remarks"
			}, {
				title : "操作",
				render : function(data) {
					return project_manage.loadOperater(data);
				}
			} ]
		});
		Table_Obj = esay;
	},
	/**
	 * 加载方法
	 * 
	 * @param data
	 * @returns {String}
	 */
	loadOperater:function(data) {
		var html = "";
		html += '<a onclick="project_manage.lookDetail(\'' + data.uuid + '\',this)">查看</a>';
		var str = data.edit_power_user_uuids.indexOf(project_manage.USER_.uuid);
		if(str!= -1||(data.liable_user_uuid==project_manage.USER_.uuid)||(project_manage.USER_.uuid=="admin")){
			html += '<a class="ml10" onclick="project_manage.edit(' + data.id + ')">编辑</a>';
		}
		// html += '<a class="ml10" onclick="project_manage.deleteThis(' + data.id + ')">删除</a>';
		return html;
	},
	/**
	 * 检索user
	 */
	searchData:function() {
		var pageSize = $("#page_size").val();
		var pageIndex = 1;
		var obj = {};
		obj.pageSize = pageSize;
		obj.pageIndex = pageIndex;
		var othr = project_manage.getParam();
		reset_serach(othr, obj);
	},
	/**
	 * 创建项目
	 */
	addSave:function(){
		parent.createPage("创建项目", "../html/project/create_project.html", true, "create_project");
//		window.location.href="/crm/html/project/create_project.html";
	},
	/**
	 * 查看详情
	 * 
	 * @param uuid project.uuid
	 */
	lookDetail:function(uuid,_this) {
		$(_this).parent().parent("tr").css("color", "red");
		parent.createPage("项目详情", "../html/project/project_detail.html?uuid=" + uuid, true, "project_detail");
//		window.location.href="/crm/html/project/project_detail.html?id=" + id;
	},
	/**
	 * 编辑项目
	 */
	edit:function(id){
		parent.createPage("编辑项目", "../html/project/edit_project.html?id=" + id, true, "edit_project");
//		window.location.href="/crm/html/project/edit_project.html?id=" + id;
	},
	/**
	 * 删除操作，已废弃，当前页面不使用
	 */
	deleteThis:function(id) {
		layer.confirm('确定要删除吗?',{btn: ['确定', '取消'],title:"提示"}, function(){
            layer.confirm('再次确认', {btn : ['确定', '取消'], title:"提示"}, function() {
                $.ajax({
                    type: "post",
                    url: "/crm/project/physicalDeleteByIds",
                    data: {
                        ids:id
                    },
                    dataType: "json",
                    async:false,
                    success:function(data) {
                        if(data.status){
                            var index=layer.alert(data.info+"请稍后刷新查看！", {icon: 1},function(){
                                project_manage.searchData();
                                layer.close(index);
                            });

                        }else{
                            layer.msg(data.info, {icon: 2});
                        }
                    }
                });
            });
	    });
	},
	beacthDelete:function() {
		var datas = Table_Obj.getCheckedItem();
		if (datas.length == 0) {
			layer.alert("未选择有效数据！", "");
			return;
		}
		var arr = new Array();
		$.each(datas, function(key, val) {
			arr[key] = val.id;
		});
		var ids = arr.join(",");
		project_manage.deleteThis(ids)
	},
	/**
	 * 导出表格
	 */
	exportExcel:function(){
		var rows = Table_Obj.getCheckedItem();
		var column = new Array();
		column.push(Table_Obj.getColumns());
		var dataList={};
		dataList.column=column;
		dataList.rows=rows;
		dataList.type="project";
		if(rows.length == 0){
			layer.alert("未选择导出的数据！");
		}else{
	        var tempForm = document.createElement("form");
	        document.body.appendChild(tempForm);
	        tempForm.method="post";
	        tempForm.action="/crm/exportExcel";
	        var hideInput = document.createElement("input");
	        hideInput.type="hidden";
	        hideInput.name= "datas";
	        hideInput.value= JSON.stringify(dataList);
	        tempForm.appendChild(hideInput);
	        tempForm.submit();
	    }
	},
	/**
	 * 导出所有查询出来的数据
	 */
	exportAllToExcel:function(){
		var pageSize = $("#total_records").text();
		var pageIndex = 1;
		var obj = project_manage.getParam();
		obj.pageSize = pageSize;
		obj.pageIndex = pageIndex;
		var column = new Array();
		column.push(Table_Obj.getColumns());
		obj.column=JSON.stringify(column);
		var tempForm = document.createElement("form");
        document.body.appendChild(tempForm);
        tempForm.method="post";
        tempForm.action="/crm/project/exportExcel";
        var hideInput = document.createElement("input");
        hideInput.type="hidden";
        hideInput.name= "datas";
        hideInput.value= JSON.stringify(obj);
        tempForm.appendChild(hideInput);
        tempForm.submit();
	}
}
